/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.tlds;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author thaidacvinh
 */
public class GridTagHandler extends SimpleTagSupport {
    private List items;
    private String fields;
    private String fieldTitles;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        
        try {
            if ((items == null) || items.isEmpty()) {
                return;
            }
            String[] fields_list = null;
            String[] field_titles_list = null;
            if (fields != null) {
                fields_list = fields.split(",");
                if ((fields_list == null) || (fields_list.length < 1)) {
                    return;
                }
                if (fieldTitles != null) {
                    field_titles_list = fieldTitles.split(",");
                    if ((field_titles_list == null) || (field_titles_list.length < 1)) {
                        return;
                    }
                    if (field_titles_list.length != fields_list.length) {
                        return;
                    }
                } else {
                    field_titles_list = fields_list;
                }
            }
            boolean all_fields = true;
            if (fields_list != null) {
                all_fields = false;
            }
            
            Iterator objects = items.iterator();
            Object o = objects.next();
            Class<?> c = o.getClass();            
            
            out.println("<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\"><tr>");
            if (all_fields) {
                for (Field field : c.getDeclaredFields()) {
                    out.println("<th>" + field.getName() + "</td>");
                }
            } else {
                for (int i = 0; i < field_titles_list.length; i++) {
                    out.println("<th>" + field_titles_list[i] + "</td>");
                }                
            }
            out.println("</tr>");
            while (true) {
                Map<String, Object> properties = BeanUtils.describe(o);
                out.println("<tr>");
                if (all_fields) {
                    for (Field field : c.getDeclaredFields()) {
                        out.println("<td>" + properties.get(field.getName()).toString() + "</td>");
                    }
                } else {
                    for (int i = 0; i < fields_list.length; i++) {
                        out.println("<td>" + properties.get(fields_list[i]).toString() + "</td>");
                    }
                }
                out.println("</tr>");
                if (objects.hasNext()) {
                    o = objects.next();
                } else {
                    break;
                }
            }
            out.println("</table>");

            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (Exception e) {
            System.err.println(e);
        }
    }

    public void setItems(List items) {
        this.items = items;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }

    public void setFieldTitles(String fieldTitles) {
        this.fieldTitles = fieldTitles;
    }
}
